"use strict";
var vpn = require('../../../../app/components/backend/service/vpn'),
    assert = require('assert'),
    helper = require('../../../helper'),
    util = require('../../../../app/components/backend/util'),
    testData = {
      ToUserName: 'gh_e8b1db57dbec',
      FromUserName: 'oCxVxs7eTWNcwP2QyGqlDXyZpbVE',
      CreateTime: 1446703501,
      MsgType: 'event',
      Event: 'CLICK',
      EventKey: 'CLICK_VPN'
    },
    weixinuserDao = require('../../../../app/components/backend/db').weixinuser,
    vpnModel = require('../../../../app/components/backend/db/models/vpn'),
    sinon   = require('sinon'),  sinonSandbox;
describe('app/components/backend/service/vpn', function () {

  // 测试前置处理
  beforeEach(function () {

      // 初始化sinon
      sinonSandbox = sinon.sandbox.create();
      return Promise.resolve();
  });

  // 测试后置处理
  afterEach(function() {

    // 复位
    if (sinonSandbox) sinonSandbox.restore();
  });

  describe('#getVpnMsg()', function () {
    it('[getVpnMsg.Nor.01].接收数据并返回xml消息', function () {

      var mockUserExpire = {
        openid: 'oNaHXsspWthiBKBda-E5C_OBP_ao',
        due_date: Date.now(),
        nickname: 'neil',
        qrcode: 'xxxxxxxxxx'
      },
      mockUserNormal = {
        openid: 'oNaHXsspWthiBKBda-E5C_OBP_ao',
        due_date: Date.now() + (1000 * 3600 * 24) + (1000 * 3600),
        nickname: 'neil',
        qrcode: 'xxxxxxxxxx'
      },
      mockUserNotRegister = {
        openid: 'oNaHXsspWthiBKBda-E5C_OBP_ao',
        due_date: 0,
        nickname: 'neil',
        qrcode: 'xxxxxxxxxx'
      };
      sinonSandbox.stub(weixinuserDao, "findByOpenId")
        .onFirstCall().returns(Promise.resolve(mockUserExpire))
        .onSecondCall().returns(Promise.resolve(mockUserNormal))
        .onThirdCall().returns(Promise.resolve(mockUserNotRegister))
        .onCall(3).returns(Promise.resolve(mockUserNormal));
      vpnModel.host = '192.168.12.88';
      vpnModel.pwd = 'xxxxxxxxxx';
      sinonSandbox.stub( util.vpnserver, "getVpn")
        .onFirstCall().returns(vpnModel)
        .onSecondCall().returns(undefined);
      var emitSpy = sinonSandbox.spy(util.sandbox, 'emit');
      return vpn.getVpnMsg(testData).then(function(data) {

        // 过期
        assert.equal(helper.getValueOfXmlNode('FromUserName', data.msg), testData.ToUserName);
        assert.equal(helper.getValueOfXmlNode('ToUserName', data.msg), testData.FromUserName);
        assert.equal(data.action_type, 'click_vpn');
        assert.ok(data.msg.indexOf('您的免费试用期已过') > 0);
        assert.equal(emitSpy.callCount, 0); // 不发消息
        return Promise.resolve();
      }).then(vpn.getVpnMsg.bind(vpn, testData)).then(function(data) {

        // 正常
        assert.equal(helper.getValueOfXmlNode('FromUserName', data.msg), testData.ToUserName);
        assert.equal(helper.getValueOfXmlNode('ToUserName', data.msg), testData.FromUserName);
        assert.equal(data.action_type, 'click_vpn');
        assert.ok(data.msg.indexOf('服务器：192.168.12.88') > 0);
        assert.ok(data.msg.indexOf('密码：xxxxxxxxxx') > 0);
        assert.ok(data.msg.indexOf('您还能免费使用1天') > 0);
        assert.equal(emitSpy.callCount, 1); // 发送消息
        assert.equal(emitSpy.args[0][0], util.sandbox.MSG.VPN_ASSIGNED);
        assert.equal(emitSpy.args[0][1].host, '192.168.12.88');
        assert.equal(emitSpy.args[0][1].openid, 'oNaHXsspWthiBKBda-E5C_OBP_ao');
        return Promise.resolve();
      }).then(vpn.getVpnMsg.bind(vpn, testData)).then(function(data) {

        // 未注册
        assert.equal(helper.getValueOfXmlNode('FromUserName', data.msg), testData.ToUserName);
        assert.equal(helper.getValueOfXmlNode('ToUserName', data.msg), testData.FromUserName);
        assert.equal(data.action_type, 'click_vpn');
        assert.ok(data.msg.indexOf('你还没有注册哦, 输入edu邮箱完成注册') > 0);
        assert.equal(emitSpy.callCount, 1); // 不发消息
        return Promise.resolve();
      }).then(vpn.getVpnMsg.bind(vpn, testData)).then(function(data) {

        // 没有可用vpn
        assert.equal(helper.getValueOfXmlNode('FromUserName', data.msg), testData.ToUserName);
        assert.equal(helper.getValueOfXmlNode('ToUserName', data.msg), testData.FromUserName);
        assert.equal(data.action_type, 'click_vpn');
        assert.ok(data.msg.indexOf('Oh, NO~~ 暂时没有可用的vpn, 请稍后再试') > 0);
        assert.equal(emitSpy.callCount, 1); // 不发消息
        return Promise.resolve();
      });
    });
  });
});